//
//  KaMiDetailVC.m
//  HengYouCai
//
//  Created by lwj on 2017/9/28.
//  Copyright © 2017年 lwj. All rights reserved.
//

#import "KaMiDetailVC.h"
#import "CDKeyModel.h"
#import "KamiTableViewCell.h"
@interface KaMiDetailVC ()
{
    BOOL _isLoseEfficacy;
    NSInteger _pageNo;
}
/**
 数据源
 */
@property (nonatomic, retain)NSMutableArray *totalArr;
@end

@implementation KaMiDetailVC

- (void)dealloc{
    NSLog(@"KaMiDetailVC dealloc");
}

- (instancetype)init
{
    if (self = [super init]) {
        //
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    [self netWorking];
    _pageNo = 1;

    self.tableView.backgroundColor = Normal_BackColor;
    //第一次不要加载 之前的界面已经有加载框
    if (![_type isEqualToString:@"1"]) {
        [self.view showHUDLoading];

    }
    [self netWorking];

    [self startRefresh];
    self.tableView.mj_footer.hidden = YES;
//    self.tableView.selec
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"KamiTableViewCell" bundle:nil] forCellReuseIdentifier:@"KaMiDetailCellID"];
}
#pragma mark- 刷新
- (void)startRefresh
{
    __weak KaMiDetailVC * weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        KaMiDetailVC * strongSelf = weakSelf;
        strongSelf -> _pageNo = 1;
        [strongSelf netWorking];
        
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf netWorking];
    }];
    
}

- (void)setType:(NSString *)type
{
    _type = type;
    //    _isLoseEfficacy = YES;
    
}
- (void)setImgLink:(NSString *)imgLink
{
    _imgLink = imgLink;
}
#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _totalArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    __weak CDKeyModel *model = _totalArr[indexPath.row];
    __weak KamiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KaMiDetailCellID" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIImageView *backImg =  [cell viewWithTag:1004];
//    [backImg sd_setImageWithURL:[NSURL URLWithString:self.imgLink] placeholderImage:[UIImage imageNamed:@"jdQuan"]];
    [backImg sd_setImageWithURL:[NSURL URLWithString:self.imgLink]];
    UIButton *copyBtn = [ cell viewWithTag:1003];
//    @weakify(self)
    [[copyBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = model.cdkey;
        
        [cell showHUDWarnTips:@"复制成功" hideDelay:1.5];
    }];
    cell.model = model;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 195.0;
}
- (NSMutableArray *)totalArr
{
    if (!_totalArr) {
        _totalArr = [NSMutableArray array];
    }
    return _totalArr;
}
- (void)netWorking
{
    NSDictionary *dic = @{@"pageNo":[NSString stringWithFormat:@"%ld",_pageNo],
                          @"pageSize":@"10",
                          @"type":_type};
    [HYCNetService getMyCDKeyListWithDict:dic OnSuccess:^(NSDictionary *dictResult) {
        //
        [self.view hideHUDLoading:0];
        NSArray *lists = dictResult[@"myCDKeyList"];
        if (_pageNo==1) {
            [_totalArr removeAllObjects];
            [self.tableView.mj_header endRefreshing];
        }
        _pageNo++;
        
        self.tableView.mj_footer.hidden = lists.count<10;
        
        [self.totalArr addObjectsFromArray:[CDKeyModel mj_objectArrayWithKeyValuesArray:lists]];
        
        [self.tableView reloadData];
    } andFailed:^(NSInteger code, NSString *errorMsg) {
        //
        [self.view showHUDFailTips:errorMsg hideDelay:kHUDHideDelay];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
    
}

@end
